from google.appengine.ext import webapp
from google.appengine.ext.webapp.util import run_wsgi_app
from google.appengine.ext.webapp import template
from google.appengine.ext import webapp
import os

from google.appengine.ext import db

class Image(db.Model):
	predicts = db.StringProperty(multiline=False);	
	name = db.StringProperty(multiline=False);
	imagetype = db.StringProperty(multiline=False);
	annotated = db.BooleanProperty();

class MainPage(webapp.RequestHandler):
	def get(self):
		query = db.Query(Image)
		
		query.filter('annotated',False);
		
		image_sets = query.fetch(10);
		template_values = {'image_sets': image_sets};
		path = os.path.join(os.path.dirname(__file__), 'index.html');
		self.response.out.write(template.render(path,template_values));
		
class Annotate(webapp.RequestHandler):
	def post(self):
		name = self.request.get('name');
		text = self.request.get('annotation')
		
		# Process this information
		
		query = db.Query(Image);
		
		query.filter('name',name);
		
		image = query.fetch(1)[0];
		image.annotated = True;
		
		image.predicts = text;
		image.put();
		
		self.response.out.write("<success>True</success>");

application = webapp.WSGIApplication([('/', MainPage),('/annotate', Annotate)],
                                     debug=True)

def main():
	run_wsgi_app(application)

if __name__ == "__main__":
	main()
